/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：后台管理员接口实现                                                    
*│　作    者：gaotang                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-01-04 09:55:38                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： BestTrader.Cms.Repository.SqlServer                                  
*│　类    名： ManagerRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using BestTrader.Cms.Core.DbHelper;
using BestTrader.Cms.Core.Options;
using BestTrader.Cms.Core.Repository;
using BestTrader.Cms.IRepository;
using BestTrader.Cms.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BestTrader.Cms.Repository.SqlServer
{
    public class ManagerRepository : BaseRepository<Manager, Int32>, IManagerRepository
    {
        public ManagerRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption = options.Get("BT.TEST");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }

        public int ChangeLockStatusById(int id, bool status)
        {
            DateTime now = DateTime.Now;
            string sql = "update [Manager] set IsLock=@IsLock,ModifyTime=@NowTime where  Id=@Id";
            return _dbConnection.Execute(sql, new
            {
                NowTime = now,
                IsLock = status ? 1 : 0,
                Id = id,
            });
        }

        public async Task<int> ChangeLockStatusByIdAsync(int id, bool status)
        {
            DateTime now = DateTime.Now;

            string sql = "update [Manager] set IsLock=@IsLock,ModifyTime=@NowTime where  Id=@Id";
            return await _dbConnection.ExecuteAsync(sql, new
            {
                NowTime=now,
                IsLock = status ? 1 : 0,
                Id = id,
            });
        }

        public int DeleteLogical(int[] ids)
        {
            DateTime now = DateTime.Now;
            string sql = "update [Manager] set IsDelete=1,ModifyTime=@NowTime where Id in @Ids";
            return _dbConnection.Execute(sql, new
            {
                NowTime = now,
                Ids = ids
            });
        }

        public async Task<int> DeleteLogicalAsync(int[] ids)
        {
            DateTime now = DateTime.Now;
            string sql = "update [Manager] set IsDelete=1,ModifyTime=@NowTime where Id in @Ids";
            return await _dbConnection.ExecuteAsync(sql, new
            {
                NowTime = now,
                Ids = ids
            });
        }

        public IEnumerable<Manager> GetCacheAllAgentManager()
        {
            string conditions = "where IsDelete=0 and State = 1 and RoleId in(3, 4)";// 未删除和已审核
            return _dbConnection.GetList<Manager>(conditions);
        }

        public bool GetLockStatusById(int id)
        {
            string sql = "select IsLock from [dbo].[Manager] where Id=@Id and IsDelete=0";
            return _dbConnection.QueryFirstOrDefault<bool>(sql, new
            {
                Id = id,
            });
        }

        public async Task<bool> GetLockStatusByIdAsync(int id)
        {
            string sql = "select IsLock from [dbo].[Manager] where Id=@Id and IsDelete=0";
            return await _dbConnection.QueryFirstOrDefaultAsync<bool>(sql, new
            {
                Id = id,
            });
        }

        public string GetNameById(int id)
        {
            string sql = "select UserName from [dbo].[Manager] where Id=@Id and IsDelete=0";
            return _dbConnection.QueryFirstOrDefault<string>(sql, new
            {
                Id = id,
            });
        }

        public string GetUserNameAndRealNameById(int id)
        {
            string sql = "select UserName+'('+RealName+')' from [dbo].[Manager] where Id=@Id and IsDelete=0";
            return _dbConnection.QueryFirstOrDefault<string>(sql, new
            {
                Id = id,
            });
        }


        public string GetRealNameByUserName(String userName)
        {
            string sql = "select RealName from [dbo].[Manager] where UserName=@userName and IsDelete=0";
            return _dbConnection.QueryFirstOrDefault<string>(sql, new
            {
                userName = userName,
            });
        }

    }
}